/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slishome;

import Server.Server;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rebecca
 */
public class SLISHOME implements Globals {
    
    

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Thread t = new Server(6060);
            t.start();

            plm.initialize();
            x10.init();

            while(true){
                String line =new ReadFromFile().ReadFile();
                if ((line != null)){
                    worker(line);
                }
                else{
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(SLISHOME.class.getName()).log(Level.SEVERE, null, ex);

                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SLISHOME.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        plm.close();
    }
    
    public static void worker(String line){
        String deviceCode = line.substring(0,1);
        String actionCode = line.substring(1,2);
        switch(deviceCode){
            case "A":
                switch(actionCode){
                    case "0":
                        x10.x10on('E',2);
                        System.out.println("Light A on");
                        break;
                    case "1":
                        x10.x10off('E',2);
                        System.out.println("Light A off");
                        break;
                    case "2":
                        x10.x10brighten('E',2);
                        System.out.println("Light A brighten");
                        break;
                    case "3":
                        x10.x10dim('E',2);
                        System.out.println("Light A dim");
                        break;                    
                }
                break;
            case "B":
                switch(actionCode){
                    case "0":
                        x10.x10on('E',3);
                        System.out.println("Light B on");
                        break;
                    case "1":
                        x10.x10off('E',3);
                        System.out.println("Light B off");
                        break;
                    case "2":
                        x10.x10brighten('E',3);
                        System.out.println("Light B brighten");
                        break;
                    case "3":
                        x10.x10dim('E',3);
                        System.out.println("Light B dim");
                        break;                    
                }
                break;
            case "C":
                switch(actionCode){
                    case "0":
                        x10.x10on('E',4);
                        System.out.println("Light C on");
                        break;
                    case "1":
                        x10.x10off('E',4);
                        System.out.println("Light C off");
                        break;
                    case "2":
                        x10.x10brighten('E',4);
                        System.out.println("Light C brighten");
                        break;
                    case "3":
                        x10.x10dim('E',4);
                        System.out.println("Light C dim");
                        break;                    
                }
                break;
            case "D":
                switch(actionCode){
                    case "1":
                        x10.thermo(3);
                        System.out.println("Thermostat off");
                        break;
                    case "4":
                        x10.thermo(1);
                        System.out.println("Thermostat Heat");
                        break;
                    case "5":
                        x10.thermo(2);
                        System.out.println("Thermostat Cool");
                        break;
                    case "6":
                        x10.thermo(4);
                        System.out.println("Thermostat Increase");
                        break;
                    case "7":
                        x10.thermo(5);
                        System.out.println("Thermostat Decrease");
                        break;
                }
                break;
            case "E":
                switch(actionCode){
                    case "1":
                        x10.setAlarmState(0);
                        System.out.println("Alarm off");
                        break;
                    case "8":
                        x10.setAlarmState(1);
                        System.out.println("Alarm Home");
                        break;
                    case "9":
                        x10.setAlarmState(2);
                        System.out.println("Alarm Away");
                         break;
                }
                break;            
        }
    }
}
